<?php

namespace common\models\help;

use Yii;
use common\helpers\Url;

/**
 * This is the model class for table "{{%help}}".
 *
 * @property int $id
 * @property int $category_id 帮助所属分类ID
 * @property string $title 标题
 * @property string $thumb 内容封面
 * @property string $content 帮助内容
 * @property int $admin_id 管理员ID
 * @property int $listorder 显示排序
 * @property int $views 浏览次数
 * @property int $recommend_at 推荐时间
 * @property string $template 特定模版
 * @property string $system 系统识别名称
 * @property int $status 状态
 * @property int $created_at 创建时间
 * @property int $updated_at 更新时间
 */
class Help extends \common\db\ActiveRecord {

    /**
     * @var integer 分类状态
     */
    const STATUS_ACTIVE = 10;
    const STATUS_DEFAULT = 0;

    /**
     * {@inheritdoc}
     */
    public static function tableName() {
        return '{{%help}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['category_id', 'admin_id', 'listorder', 'recommend_at', 'views', 'status', 'created_at', 'updated_at'], 'integer'],
            [['title', 'content'], 'required'],
            [['content'], 'string'],
            [['title'], 'string', 'max' => 64],
            [['template', 'system'], 'string', 'max' => 50],
            [['thumb'], 'string', 'max' => 255],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'category_id' => '帮助所属分类ID',
            'title' => '标题',
            'thumb' => '内容封面',
            'content' => '帮助内容',
            'admin_id' => '管理员ID',
            'listorder' => '显示排序',
            'recommend_at' => '推荐时间',
            'views' => '推荐时间',
            'template' => '指定模板',
            'system' => '系统识别名称',
            'status' => '状态',
            'created_at' => '创建时间',
            'updated_at' => '更新时间',
        ];
    }

    public function getCategory() {
        return $this->hasOne(HelpCategory::className(), ['id' => 'category_id']);
    }

    public function getCover() {
        return Url::attach($this->thumb);
    }

    public function getLink() {
        return Url::weblink('help/view/{id}', [
            'id' => $this->id
        ]);
    }

    /**
     * {@inheritdoc}
     */
    public function fields() {
        return [
            'id',
            'title',
            'views',
            'content',
            'link',
        ];
    }

    public static function findBySystem($name) {
        return static::find()->andWhere([
            'system' => $name
        ])->all();
    }

}
